'use strict';

angular.module('myApp.camera', ['ngRoute'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/testing/camera', {
            templateUrl: 'pages/testing/camera/camera.html',
            // template: '<video src=""></video>',
            controller: 'CameraCtrl'
        });
    }])

    .controller('CameraCtrl', function ($rootScope, $scope, $location) {
        $rootScope.setStatusBar(true)
        $rootScope.items = []
        $rootScope.title = '摄像头测试'
        $scope.camera_id = 0

        const localMenus = [
            {
                text: '全屏',
                callback: (index) => {
                    console.log(`menu item-${index} clicked`)
                    const video = document.querySelector('#ros_img')
                    if (video.classList.contains('horizontal')) {
                        $rootScope.show_header = true
                        $rootScope.show_footer = true
                        $rootScope.updatePageInfo()
                        video.classList.remove('horizontal')
                        video.classList.add('vertical')
                    } else {
                        $rootScope.show_header = false
                        $rootScope.show_footer = false
                        $rootScope.updatePageInfo()
                        video.classList.add('horizontal')
                        video.classList.remove('vertical')
                    }
                }
            },
            {
                text: '切换摄像头',
                callback: async (index) => {
                    $rootScope.ros.setCameraUpdateFrequence(15)
                    await $rootScope.ros.cameraSetEnable(0)
                    if($scope.camera_id == 0){
                        $scope.camera_id = 1
                    }else{
                        $scope.camera_id = 0
                    }
                    await $rootScope.ros.cameraSetEnable(1, $scope.camera_id)
                }
            },
            // {
            //     text: '关闭摄像头',
            //     callback: (index) => {
            //         $rootScope.ros.cameraSetEnable(0)
            //     }
            // }
        ]

        $rootScope.localMenus[$location.path()] = localMenus
        $rootScope.updatePageInfo()

        var image = document.getElementById("ros_img");

        function onImageMsg(message) {
            console.log(message)
            var data = "data:image/jpeg;base64," + message.data;
            image.setAttribute('src', data)
        }
        let topic = "/ubiquityrobot/camera_node/image_raw/compressed"
        let listener = $rootScope.ros.subCompressedImage(topic, onImageMsg)

        $scope.$on('$routeChangeStart', function ($event, next, current) {
            // console.log('$routeChangeStart', $event, next, current)
            if (listener) {
                try {
                    listener.unsubscribe()
                    // this.listener = null
                } catch (error) {

                }
            }
            $rootScope.ros.cameraSetEnable(0)
        });



        $rootScope.ros.setCameraUpdateFrequence(15)
        $rootScope.ros.cameraSetEnable(1)

    });